import Menu from "./Menu";
import Utils from "../utils";
import GameConst from "../GameConst";
import ClickableObject from "../ClickableObject";
//import MenuManager from "../menumanager";

export default class PopupMenu extends Menu
{
    constructor(name){
        super();
        
        this.showDelay = 0;
        this.targetPos = GameConst.FULL_SCREEN_H/2-200;
        var sprite = new Laya.Sprite;
        this.sprite = sprite;
        sprite.y = 0;
       
        var grayImg = Utils.createSprite("menu/mengban.png");
        grayImg.width = 100 + 2*GameConst.SCREEN_W;
        grayImg.height = 100 + 2*GameConst.FULL_SCREEN_H;
        grayImg.y = -50;
        grayImg.x = -50;
        this.grayBack = grayImg;
        sprite.addChild(grayImg);
        
        
        var pjson = Laya.loader.getRes('prefabs/'+name+'.json');

        var prefabs = new Laya.Prefab();
        prefabs.json = pjson;
        var mainUI = prefabs.create();
        var baseSprite = new Laya.Sprite;
        sprite.addChild(baseSprite);
        baseSprite.addChild(mainUI);
        sprite.name = "menu_panel";
        this.baseSprite = baseSprite;
        this.ui = mainUI;
        // this.brandLogo = new LogoObject(n,GameConst.SCREEN_W - 90,GameConst.CURRENT_SHIFT +GameConst.SCREEN_H - 30 + t, 1);
        // if (this.brandLogo.sprite)
        //     this.brandLogo.sprite.y =GameConst.ACTUAL_H - 40 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + t;
        // this.moreGames = new MoreGamesButton(n, 90,GameConst.CURRENT_SHIFT +GameConst.SCREEN_H - 30 + t, 1);
        // if (this.moreGames.sprite)
        //     this.moreGames.sprite.y =GameConst.ACTUAL_H - 60 + (App.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + t

        var btn = mainUI.getChildByName('btn_home');
        if(btn){
            var obj = new ClickableObject(btn);
            obj.setCircle(60,60,60);
            obj.callback = this.loadMainMenu;
            this.clickables.push(obj);
        }

        btn = mainUI.getChildByName('btn_replay');
        if(btn){
            var obj = new ClickableObject(btn);
            obj.setCircle(60,60,60);
            obj.callback = this.restartLevel;
            this.clickables.push(obj);
        }
        btn = mainUI.getChildByName('btn_play');
        if(btn){
            var obj = new ClickableObject(btn);
            obj.setCircle(60,60,60);
            obj.callback = this.onPlayDown;
            this.clickables.push(obj);
        }
    }

    show() {
        super.show();
        Laya.stage.addChild(this.sprite);
        Laya.Tween.clearAll(this.baseSprite);
        Laya.Tween.clearAll(this.grayBack);
        
        this.grayBack.alpha = 0;
        this.baseSprite.y = -250;
        Laya.Tween.to(this.grayBack,{
            alpha: 1
        }, 200 * 1.3, Laya.Ease.cubicInOut);
        Laya.Tween.to(this.baseSprite, {
            y: this.targetPos
        }, 1e3 * 1.3, Laya.Ease.elasticOut);
        // if (this.clickables.indexOf(this.brandLogo) == -1)
        //     this.clickables.push(this.brandLogo);
        // if (this.clickables.indexOf(this.moreGames) == -1)
        //     this.clickables.push(this.moreGames);
        // if (this.brandLogo.sprite)
        //     this.brandLogo.sprite.visible = true;
        // if (this.moreGames.sprite)
        //     this.moreGames.sprite.visible = true
    };
    hide() {
        var t = this;
        if (this.needToHideInstantly()) {
            super.hide();
            return
        }
        Laya.Tween.clearAll(this.baseSprite);
        Laya.Tween.clearAll(this.grayBack);
        this.grayBack.alpha = 1;
        this.baseSprite.y = this.targetPos;
        Laya.Tween.to(this.grayBack,{
            alpha: 0
        }, 750, Laya.Ease.cubicInOut);
        Laya.Tween.to(this.baseSprite, {
            y: -550
        }, 750, Laya.Ease.elasticInOut,Laya.Handler.create(this,super.hide));
        // if (this.brandLogo.sprite)
        //     this.brandLogo.sprite.visible = false;
        // if (this.moreGames.sprite)
        //     this.moreGames.sprite.visible = false
    };
    needToHideInstantly() {
        return GameConst.menuManager.transition.menuToShow == null && GameConst.menuManager.transition.isActive
    };
}
    

